#include "zigbeedatacontrol.h"
#include <QTextStream>
#include <QStringList>

ZigBeeDataControl::ZigBeeDataControl(QObject *parent) :
    QObject(parent)
{
}
void ZigBeeDataControl::slt_data(const SerialPortData &data)
{
    switch(data.cmd) {
    case SENSOR:sersor(data.cmd,data.data);break;
    case NETBUILD:
    case UPDATA:netTopologyBuild(data.type,data.data);break;
    case TEST:netTopologyTest(data.data);break;
    case COORDINATOR_TEST:CoordinatorTest(data.data);break;
    default:break;
    }
}
void ZigBeeDataControl::sersor(int flag, const QByteArray &data)
{


#ifndef NEW_DATA_STYLE
    QList<QByteArray> list = data.split('|');
    if (list.length() < 3) {

    } else {

        QString addr = list.at(0);
        ushort name = static_cast<ushort>(list.at(2).at(0));//设备名
        int value = list.at(1).toInt();



        // emit sig_sensorData(name,flag,value);
        emit sig_sensorData(addr,value);

        emit sig_items(addr,name,value,flag);


        emit sig_clear0(name);

        emit sig_sensorData(name,flag,value);


        if (!m_dataFilter.filtering(flag,value)) {
            emit sig_badData(name,flag,value);
        }


        qDebug() << "传感器数据" << addr << name << value;

        QString info = trUtf8("传感器数据-->设备名:%1 值:%2").arg(name).arg(value);
        qDebug() << info;
        emit sig_realTimeInfo(info);

    }
#else

//    float t = (data.at(0) - '0') * 10 + (data.at(1) - '0') + (data.at(2) - '0') * 0.1;
//    QString t = QString::number(t);
//    if ()
#endif

}

/*
 *设计一段格式
 *本节点：父节点:设备名
 *
 *
 */
void ZigBeeDataControl::netTopologyBuild(char type,const QByteArray &data)
{
    QList<QByteArray> list = data.split('|');
    if (list.length() < 3) return;
    QString addr = list.at(0);
    QString parentAddr = list.at(1);
    ushort name = (ushort)list.at(2).at(0);


    emit sig_netTopologyBuild(type,addr,parentAddr,name);

    qDebug() << "网络建立" << addr << parentAddr << name;


    QString info = trUtf8("网络组建-->设备类型:%1 设备名:%2 节点地址:%3 父节点地址:%4").
            arg(type).arg(name).arg(addr.toUtf8().data()).arg(parentAddr);
    qDebug() << info;
    emit sig_realTimeInfo(info);


}
/**
 * @brief ZigBeeDataControl::netTopologyTest
 * @param data
 */
void ZigBeeDataControl::netTopologyTest(const QByteArray &data)
{
    QList<QByteArray> list = data.split('|');
    if (list.length() < 3) return;
    QString addr = list.at(0);
    ushort name = (ushort)list.at(2).at(0);
    emit sig_netTopologyTest(addr,name);

    qDebug() << "网络测试" << addr << name;
}

void ZigBeeDataControl::slt_addAfilte(const Filter &f)
{
    m_dataFilter.addAFilter(f);
}
/**
 * @brief ZigBeeDataControl::readSettings 没有实现，关于对每个节点的数据过滤可能使用数据库
 */
void ZigBeeDataControl::readSettings()
{
    QFile file("filte.conf");
    if (file.open(QIODevice::ReadWrite)) {
        QTextStream in(&file);
        QString str = in.readLine();
        while (!str.isNull()) {
            QStringList list = str.split(' ');
            Filter f(list.at(0).toInt(),list.at(1).toInt(),list.at(2).toInt());
            m_dataFilter.addAFilter(f);
            str = in.readLine();
        }
    }
    file.close();
}

#ifdef TEST_NET

void ZigBeeDataControl::test()
{
    emit sig_netTopologyBuild('C',"0000","",'A');
    emit sig_netTopologyBuild('R',"0001","0000",'B');
    emit sig_netTopologyBuild('E',"0002","0001",'C');
    emit sig_netTopologyBuild('E',"0003","0001",'D');

    emit sig_netTopologyBuild('R',"0004","0001",'E');
    emit sig_netTopologyBuild('E',"0005","0004",'F');
    emit sig_netTopologyBuild('E',"0006","0004",'G');

    emit sig_netTopologyBuild('E',"0003","0004",'D');

    emit sig_netTopologyBuild('R',"0004","0000",'E');

    //  emit sig_netTopologyBuild('C',"0000","",'A');
    //  emit sig_netTopologyBuild('C',"0000","",'A');











}

#endif


//这边发送测试成功
void ZigBeeDataControl::CoordinatorTest(const QByteArray &data)
{
    Q_UNUSED(data);
    emit sig_coordinatorTest();

}
